﻿package Leophy.text {
	import flash.text.TextRenderer;	
	import flash.text.AntiAliasType;	
	import flash.events.Event;
	import flash.text.TextField;	
	import flash.text.TextFormat;

	import Leophy.Interfaces.IAsynCallBack;
	//import Leophy.Interfaces.IFont;
	import Leophy.Interfaces.ILanguage;
	import Leophy.Interfaces.IResource;
	//import Leophy.Interfaces.IResourceManager;
	//import Leophy.Management.ResourceData;
	import Leophy.Management.ResourceManager;

	/**
	 * ...
	 * @author TOPHY
	 */
	public class MyTextField extends TextField implements IResource, IAsynCallBack {

		public static var M_SOURCE_STYLE_CHANGED : String = "onSourceStyleChanged";
		private var _sourceID : String;

		public function MyTextField(sourceID : String = "0") {
			//var tf : TextFormat = new TextFormat();
			_sourceID = sourceID;
			super();
			init();
		}

		private function init() : void {
			TextRenderer.maxLevel = 7;
			this.antiAliasType = AntiAliasType.ADVANCED;
		}

		public function bindLanguage(language : Language) : void {
			if (language != null) {
				language.addEventListener(Language.M_LANGUAGE_CHANGED, changedLanguage);
				language.SetTextField(this, this._sourceID);
			}
		}

		private function changedLanguage(ev : Event = null) : void {
			var ilgg : ILanguage;
			if (ev == null) {
				ilgg = Language.Ins;
			}else {
				ilgg = ILanguage(ev.target);
			}
			ilgg.SetTextField(this, this._sourceID);
		}

		///=======================================================================  interface 的实现
		/* INTERFACE Leophy.Interfaces.IResource */
		public function onLanguageChange(ev : Event) : void {
			var ilgg : ILanguage;
			if (ev == null) {
				ilgg = Language.Ins;
			}else {
				ilgg = ILanguage(ev.target);
			}
			changedLanguage(ev);
			ResourceManager.Ins.BeginSetStyle(this, ilgg.CurrentLanguage);
		}

		/* INTERFACE Leophy.Interfaces.IAsynCallBack */
		public function EndAsynCallBack(type : String, data : * ) : void {
			
			if (type == "setstyle") {
				if(data[0] != null) {
					var tf : TextFormat = TextFormat(data[0]);
					
					if (data[1]) {
						tf.font = MyFont.Ins.GetEmbedFontByName(tf.font).fontName;
					}
					this.embedFonts = data[1];
					this.setTextFormat(tf);
					this.defaultTextFormat = tf;
					
					this.dispatchEvent(new Event(M_SOURCE_STYLE_CHANGED));
				}
			}
		}

		/* INTERFACE Leophy.Interfaces.IResource */
		public function getSourceId() : String {
			return this._sourceID;
		}
	}
}